package game.player;

import java.awt.Color;
import java.util.Random;

import game.driver.Controller;
import game.driver.GameLogic;
import game.model.Card;

/**
 * This class will make the AI player to play with the right strategy and will
 * make functions to allow the computer to play.
 * 
 * @author Maria
 * 
 */
public class AIPlayer extends Player {

	GameLogic game = new GameLogic();
	Random rand = new Random();
	

	/**
	 * Constructor
	 * 
	 * @param control
	 * @param nm
	 */
	public AIPlayer(Controller control, String nm, int d) {
		super(control, nm,d);
		
		// TODO Auto-generated constructor stub
	}

	// *** Method where the move of the AI player will be done and the strategy
	// it will be use.**@return*/

	public Card makePlay(Card c) {
		System.out.print("thinking");

		Card car = null;

		for (int i = 0; i < mycards.size(); i++) {

			if (mycards.get(i).compareValue(c) == 0)

				car = mycards.get(i);

			else if (mycards.get(i).compareColor(c))

				car = mycards.get(i);

			else if (mycards.size() < 3 && matchWildfour(c))

				car = mycards.get(i);

			else if (mycards.size() < 3 && matchWild(c))

				car = mycards.get(i);

			else
				car=null;
				

		
System.out.print(car);
			

			

		}

		

		return car;

	}

	

	/**
	 * Compare Ai player cards with top card and see if matches the color.
	 * 
	 * @return
	 */
	public boolean matchColor() {
		boolean num = false;
		for (int i = 0; i < mycards.size(); i++) {
			if (mycards.get(i).equals(control.seeTopCard())) {

				num = true;
			} else
				num = false;
		}
		return num;
	}

	/**
	 * Compare Ai player cards with top card and see if matches the number
	 * 
	 * @return
	 */
	public boolean matchNum() {
		boolean num = false;
		for (int i = 0; i < mycards.size(); i++) {
			if (mycards.get(i).equals(control.seeTopCard())) {

				num = true;
			} else
				num = false;
			;
		}
		return num;
	}

	/**
	 * 
	 * 
	 * Compare Ai player cards with top card and see if it have the Wild Four
	 * card.
	 * 
	 * @param c
	 * @return
	 */
	public boolean matchWildfour(Card c) {
		if (mycards.contains(game.isWildDrawFour(c)))
			return true;
		else
			return false;
	}

	/**
	 * 
	 * 
	 * Compare Ai player cards with top card and see if it have the Wild card.
	 * 
	 * @param c
	 * @return
	 */
	public boolean matchWild(Card c) {
		if (mycards.contains(game.isWild(c)))
			return true;
		else
			return false;
	}

	/**
	 * 
	 * 
	 * Compare Ai player cards with top card and see if it have the reverse
	 * card.
	 * 
	 * @param c
	 * @return
	 */
	public boolean matchReverse(Card c) {
		if (mycards.contains(game.isReverse(c)
				&& control.seeTopCard().equals(matchColor())))
			return true;
		else
			return false;
	}

	/**
	 * 
	 * 
	 * Compare Ai player cards with top card and see if it have the skip card.
	 * 
	 * @param c
	 * @return
	 */
	public boolean matchSkip(Card c) {
		if (mycards.contains(game.isSkip(c)
				&& control.seeTopCard().equals(matchColor())))
			return true;
		else
			return false;
	}

	/**
	 * 
	 * 
	 * Compare Ai player cards with top card and see if it have the skip card.
	 * 
	 * @param c
	 * @return
	 */
	public boolean matchdrawn2(Card c) {
		if (mycards.contains(game.isDrawTwo(c)
				&& control.seeTopCard().equals(matchColor())))
			return true;
		else
			return false;
	}

	public void selectWildColor() {

		if (myYellow.size() > myBlue.size() && myYellow.size() > myGreen.size()
				&& myYellow.size() > myRed.size())

			for (int i = 0; i < mycards.size(); i++) {

				if (mycards.get(i).equals(Color.YELLOW))

					control.setWildColor(Color.YELLOW);

			}

		else if (myBlue.size() > myGreen.size()
				&& myBlue.size() > myYellow.size()
				&& myBlue.size() > myRed.size()
				&& myYellow.size() > myBlue.size())

			for (int i = 0; i < mycards.size(); i++) {

				if (mycards.get(i).equals(Color.BLUE))

					control.setWildColor(Color.BLUE);

			}

		else if (myBlue.size() < myGreen.size()
				&& myGreen.size() > myYellow.size()
				&& myGreen.size() > myRed.size()
				&& myGreen.size() > myBlue.size())

			for (int i = 0; i < mycards.size(); i++) {

				if (mycards.get(i).equals(Color.GREEN))

					control.setWildColor(Color.GREEN);

			}

		else if (myRed.size() > myGreen.size()
				&& myRed.size() > myYellow.size()
				&& myRed.size() > myRed.size() && myRed.size() > myBlue.size())

			for (int i = 0; i < mycards.size(); i++) {

				if (mycards.get(i).equals(Color.RED))

					control.setWildColor(Color.RED);

			}

	}

	/**
	 * 
	 * 
	 * Determine the time the AI player will take to press the uno bottom
	 * depending on the difficulty.
	 * 
	 * @return
	 */

	public long unoTime() {

		long random = 0;

		if (getDifficulty() == 0) {

			random = (long) (1.25 + (long) (Math.random() / 4));

		}

		else if (getDifficulty() == 1) {

			random = (long) (.75 + (long) (Math.random() / 4));

		}

		else if (getDifficulty() == 2) {

			random = (long) (.25 + (long) (Math.random() / 4));

		}

		return random;

	}

public Card play(Card c){
	for(int i=0;i<mycards.size();i++){
	if(mycards.get(i).compareMatch(c))
		return mycards.get(i);
	else if(mycards.get(i).equals(Color.BLACK))
		return mycards.get(i);
	
	}
	
	return null;
}


}